package com.ourteam.product.model.api;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.BooleanUtils;

import com.ourteam.product.model.dao.IProductModelObjectRelateDAO;
import com.ourteam.product.model.dao.ProductModelActionDAOQueryBean;
import com.ourteam.product.model.dao.ProductModelObjectRelateDAOQueryBean;
import com.ourteam.product.model.dao.ProductModelPropertyRelateDAOQueryBean;
import com.ourteam.product.model.dao.ProductModelServiceDAOQueryBean;
import com.ourteam.product.model.domain.ProductModelActionBean;
import com.ourteam.product.model.domain.ProductModelObjectBean;
import com.ourteam.product.model.domain.ProductModelObjectRelateBean;
import com.ourteam.product.model.domain.ProductModelPropertyBean;
import com.ourteam.product.model.domain.ProductModelRelateTypeEnum;
import com.ourteam.product.model.domain.ProductModelServiceBean;
import com.ourteam.product.model.service.IProductModelService;
import com.ourteam.product.model.service.ProductModelServiceFactory;

public class ProductModelObjectHelper {

	static final IProductModelService PRODUCT_MODEL_SERVICE = ProductModelServiceFactory.getProductModelService();

	private ProductModelObjectBean modelObjectBean;

	public ProductModelObjectHelper(ProductModelObjectBean modelObjectBean) {
		super();
		this.modelObjectBean = modelObjectBean;
	}

	public static ProductModelObjectHelper getInstance(long id) throws Exception {
		return new ProductModelObjectHelper(PRODUCT_MODEL_SERVICE.getProductModelObjectById(id));
	}

	public long getId() {

		return this.modelObjectBean.getId();
	}

	public String getDomianName() {
		if (modelObjectBean == null) {
			return null;
		}
		return modelObjectBean.getDomainName();
	}

	public String getPackageName() {
		if (modelObjectBean == null) {
			return null;
		}
		return modelObjectBean.getPackageName();
	}

	public String getObjectName() {
		if (modelObjectBean == null) {
			return null;
		}
		return modelObjectBean.getObjectName();
	}

	public String getObjectCode() {
		if (modelObjectBean == null) {
			return null;
		}
		if(modelObjectBean.getPackageId() != 0) {
			return modelObjectBean.getPackageName() + "." + modelObjectBean.getObjectName();
		}
		else {
			return modelObjectBean.getObjectName();
		}
	}

	public String getObjectType() {
		if (modelObjectBean == null) {
			return null;
		}
		return modelObjectBean.getObjectType();
	}

	public ProductModelObjectHelper getMappedTable() throws Exception {

		ProductModelObjectBean[] objectBeans = PRODUCT_MODEL_SERVICE.getRelatedProductModelObjects(
				this.modelObjectBean.getId(),
				new ProductModelRelateTypeEnum[] { ProductModelRelateTypeEnum.MAP_TABLE });

		if (ArrayUtils.isEmpty(objectBeans)) {
			return null;
		} else {
			return new ProductModelObjectHelper(objectBeans[0]);
		}

	}

	public ProductModelObjectRelateHelper getMappedTableRelation() throws Exception {

		ProductModelObjectRelateDAOQueryBean queryBean = new ProductModelObjectRelateDAOQueryBean();

		queryBean.createCriteria().andObjectIdEqualTo(this.modelObjectBean.getId())
				.andRelatedTypeEqualTo(ProductModelRelateTypeEnum.MAP_TABLE.getName());

		ProductModelObjectRelateBean[] objectRelateBeans = PRODUCT_MODEL_SERVICE
				.queryProductModelObjectRelates(queryBean);

		if (ArrayUtils.isEmpty(objectRelateBeans)) {
			return null;
		} else {
			return new ProductModelObjectRelateHelper(objectRelateBeans[0]);
		}

	}

	public ProductModelObjectRelateHelper getMappedDAORelation() throws Exception {

		ProductModelObjectRelateDAOQueryBean queryBean = new ProductModelObjectRelateDAOQueryBean();

		queryBean.createCriteria().andObjectIdEqualTo(this.modelObjectBean.getId())
				.andRelatedTypeEqualTo(ProductModelRelateTypeEnum.MAP_DAO.getName());

		ProductModelObjectRelateBean[] objectRelateBeans = PRODUCT_MODEL_SERVICE
				.queryProductModelObjectRelates(queryBean);

		if (ArrayUtils.isEmpty(objectRelateBeans)) {
			return null;
		} else {
			return new ProductModelObjectRelateHelper(objectRelateBeans[0]);
		}

	}

	public ProductModelObjectHelper getMappedDAO() throws Exception {

		ProductModelObjectBean[] objectBeans = PRODUCT_MODEL_SERVICE.getRelatedProductModelObjects(
				this.modelObjectBean.getId(), new ProductModelRelateTypeEnum[] { ProductModelRelateTypeEnum.MAP_DAO });

		if (ArrayUtils.isEmpty(objectBeans)) {
			return null;
		} else {
			return new ProductModelObjectHelper(objectBeans[0]);
		}

	}

	public ProductModelObjectHelper[] getDepends() throws Exception {

		ProductModelObjectBean[] objectBeans = PRODUCT_MODEL_SERVICE.getRelatedProductModelObjects(
				this.modelObjectBean.getId(), new ProductModelRelateTypeEnum[] { ProductModelRelateTypeEnum.DEPEND });

		ProductModelObjectHelper[] helpers = new ProductModelObjectHelper[objectBeans.length];

		for (int i = 0; i < helpers.length; i++) {
			helpers[i] = new ProductModelObjectHelper(objectBeans[i]);

		}

		return helpers;

	}

	public ProductModelObjectRelateHelper[] getDependRelations() throws Exception {

		ProductModelObjectRelateDAOQueryBean queryBean = new ProductModelObjectRelateDAOQueryBean();

		queryBean.createCriteria().andObjectIdEqualTo(this.modelObjectBean.getId())
				.andRelatedTypeEqualTo(ProductModelRelateTypeEnum.DEPEND.getName());

		ProductModelObjectRelateBean[] objectRelateBeans = PRODUCT_MODEL_SERVICE
				.queryProductModelObjectRelates(queryBean);

		ProductModelObjectRelateHelper[] objectRelateHelpers = new ProductModelObjectRelateHelper[objectRelateBeans.length];

		for (int i = 0; i < objectRelateHelpers.length; i++) {
			objectRelateHelpers[i] = new ProductModelObjectRelateHelper(objectRelateBeans[i]);
		}

		return objectRelateHelpers;

	}

	public ProductModelObjectRelateHelper[] getRelatedQueryRelations() throws Exception {

		ProductModelObjectRelateDAOQueryBean queryBean = new ProductModelObjectRelateDAOQueryBean();

		queryBean.createCriteria().andObjectIdEqualTo(this.modelObjectBean.getId())
				.andRelatedTypeEqualTo(ProductModelRelateTypeEnum.RELATE_QUERY.getName());

		queryBean.setSortProperty(IProductModelObjectRelateDAO.SortIndex);

		ProductModelObjectRelateBean[] objectRelateBeans = PRODUCT_MODEL_SERVICE
				.queryProductModelObjectRelates(queryBean);

		ProductModelObjectRelateHelper[] objectRelateHelpers = new ProductModelObjectRelateHelper[objectRelateBeans.length];

		for (int i = 0; i < objectRelateHelpers.length; i++) {
			objectRelateHelpers[i] = new ProductModelObjectRelateHelper(objectRelateBeans[i]);
		}

		return objectRelateHelpers;

	}

	public ProductModelObjectHelper[] getRelatedQuery() throws Exception {

		ProductModelObjectBean[] objectBeans = PRODUCT_MODEL_SERVICE.getRelatedProductModelObjects(
				this.modelObjectBean.getId(),
				new ProductModelRelateTypeEnum[] { ProductModelRelateTypeEnum.RELATE_QUERY });

		ProductModelObjectHelper[] helpers = new ProductModelObjectHelper[objectBeans.length];

		for (int i = 0; i < helpers.length; i++) {
			helpers[i] = new ProductModelObjectHelper(objectBeans[i]);

		}

		return helpers;

	}

	public ProductModelObjectHelper[] getRelatedOperation() throws Exception {

		ProductModelObjectBean[] objectBeans = PRODUCT_MODEL_SERVICE.getRelatedProductModelObjects(
				this.modelObjectBean.getId(),
				new ProductModelRelateTypeEnum[] { ProductModelRelateTypeEnum.RELATE_OPERATION });

		ProductModelObjectHelper[] helpers = new ProductModelObjectHelper[objectBeans.length];

		for (int i = 0; i < helpers.length; i++) {
			helpers[i] = new ProductModelObjectHelper(objectBeans[i]);

		}

		return helpers;

	}

	public ProductModelObjectRelateHelper[] getRelatedOperationRelations() throws Exception {

		ProductModelObjectRelateDAOQueryBean queryBean = new ProductModelObjectRelateDAOQueryBean();

		queryBean.createCriteria().andObjectIdEqualTo(this.modelObjectBean.getId())
				.andRelatedTypeEqualTo(ProductModelRelateTypeEnum.RELATE_OPERATION.getName());

		ProductModelObjectRelateBean[] objectRelateBeans = PRODUCT_MODEL_SERVICE
				.queryProductModelObjectRelates(queryBean);

		ProductModelObjectRelateHelper[] objectRelateHelpers = new ProductModelObjectRelateHelper[objectRelateBeans.length];

		for (int i = 0; i < objectRelateHelpers.length; i++) {
			objectRelateHelpers[i] = new ProductModelObjectRelateHelper(objectRelateBeans[i]);
		}

		return objectRelateHelpers;

	}

	public ProductModelPropertyHelper[] getProperties() throws Exception {

		ProductModelPropertyBean[] modelPropertyBeans = PRODUCT_MODEL_SERVICE
				.getProductModelObjectProperties(this.modelObjectBean.getId());

		ProductModelPropertyHelper[] propertyHelpers = new ProductModelPropertyHelper[modelPropertyBeans.length];

		for (int i = 0; i < propertyHelpers.length; i++) {
			propertyHelpers[i] = new ProductModelPropertyHelper(modelPropertyBeans[i]);

		}
		return propertyHelpers;
	}

	public ProductModelPropertyHelper[] getNotMappedProperties() throws Exception {

		ProductModelPropertyBean[] modelPropertyBeans = PRODUCT_MODEL_SERVICE.getProductModelNotRelatedPropertiesByType(
				this.modelObjectBean.getId(), new ProductModelRelateTypeEnum[] { ProductModelRelateTypeEnum.MAP_TABLE,
						ProductModelRelateTypeEnum.MAP_DAO, ProductModelRelateTypeEnum.RELATE_QUERY });

		ProductModelPropertyHelper[] propertyHelpers = new ProductModelPropertyHelper[modelPropertyBeans.length];

		for (int i = 0; i < propertyHelpers.length; i++) {
			propertyHelpers[i] = new ProductModelPropertyHelper(modelPropertyBeans[i]);

		}

		return propertyHelpers;
	}

	public ProductModelPropertyHelper[] getPrimaryProperties() throws Exception {

		ProductModelPropertyBean[] modelPropertyBeans = PRODUCT_MODEL_SERVICE
				.getProductModelObjectProperties(this.modelObjectBean.getId());

		List<ProductModelPropertyHelper> propertyHelpers = new ArrayList<ProductModelPropertyHelper>();

		for (int i = 0; i < modelPropertyBeans.length; i++) {
			if (BooleanUtils.toBoolean(modelPropertyBeans[i].getIsPrimaryKey())) {
				propertyHelpers.add(new ProductModelPropertyHelper(modelPropertyBeans[i]));
			}

		}

		return propertyHelpers.toArray(new ProductModelPropertyHelper[propertyHelpers.size()]);
	}

	public boolean isPropertyMapped(ProductModelPropertyBean propertyBean) throws Exception {

		ProductModelPropertyRelateDAOQueryBean queryBean = new ProductModelPropertyRelateDAOQueryBean();

		queryBean.createCriteria().andPropertyIdEqualTo(propertyBean.getId())
				.andRelatedTypeIn(new String[] { "MAP_TABLE" });

		return PRODUCT_MODEL_SERVICE.queryProductModelPropertyRelateCount(queryBean) > 0;

	}

	public ProductModelActionBean[] getQueryActions() throws Exception {
		ProductModelActionDAOQueryBean queryBean = new ProductModelActionDAOQueryBean();

		queryBean.createCriteria().andActionEqualTo("query").andObjectIdEqualTo(this.modelObjectBean.getId());

		ProductModelActionBean[] actionBeans = PRODUCT_MODEL_SERVICE.queryProductModelActions(queryBean);

		return actionBeans;
	}

	public ProductModelActionBean[] getCreateActions() throws Exception {
		ProductModelActionDAOQueryBean queryBean = new ProductModelActionDAOQueryBean();

		queryBean.createCriteria().andActionEqualTo("insert").andObjectIdEqualTo(this.modelObjectBean.getId());

		ProductModelActionBean[] actionBeans = PRODUCT_MODEL_SERVICE.queryProductModelActions(queryBean);

		return actionBeans;
	}

	public ProductModelActionBean[] getDeleteActions() throws Exception {
		ProductModelActionDAOQueryBean queryBean = new ProductModelActionDAOQueryBean();

		queryBean.createCriteria().andActionEqualTo("delete").andObjectIdEqualTo(this.modelObjectBean.getId());

		ProductModelActionBean[] actionBeans = PRODUCT_MODEL_SERVICE.queryProductModelActions(queryBean);

		return actionBeans;
	}

	public ProductModelActionBean[] getUpdateActions() throws Exception {
		ProductModelActionDAOQueryBean queryBean = new ProductModelActionDAOQueryBean();

		queryBean.createCriteria().andActionEqualTo("update").andObjectIdEqualTo(this.modelObjectBean.getId());

		ProductModelActionBean[] actionBeans = PRODUCT_MODEL_SERVICE.queryProductModelActions(queryBean);

		return actionBeans;
	}

	public ProductModelServiceHelper[] getServiceObjects() throws Exception {

		ProductModelServiceDAOQueryBean queryBean = new ProductModelServiceDAOQueryBean();

		queryBean.createCriteria().andObjectIdEqualTo(this.modelObjectBean.getId());

		ProductModelServiceBean[] serviceBeans = PRODUCT_MODEL_SERVICE.queryProductModelServices(queryBean);

		ProductModelServiceHelper[] helpers = new ProductModelServiceHelper[serviceBeans.length];

		for (int i = 0; i < helpers.length; i++) {
			helpers[i] = new ProductModelServiceHelper(serviceBeans[i]);
		}

		return helpers;
	}

}
